<?php

require_once 'AFaccount.model.php';
require_once 'UTerrorcode.php';

class AccountController {
//    for next refactor
//    public $username;
//    public $password;

    public function requestHandler($arr) {
        $output = array(ErrorCode::failRead);

        $accountMO = Account::init($arr);
        if (!empty($accountMO)) {
            if (!empty($arr[':password'])) {
                //gets password only
                $output = $accountMO[0]->password;
            } else if (!empty($arr[':empStatus'])) {
                //gets empstatus only
                $output = $accountMO[0] -> empStatus;
            } else if (!empty($arr[':accName'])) {
                //get One Account
                $digest = hash('md5', ($accountMO[0]->accName) . ($accountMO[0]->password));
                $item = array(
                    'accName'  => $accountMO[0]->accName,
                    'dataHash' => $digest
                );
                $output = array('errorCode' => ErrorCode::successRead, 'results' => $item);
            } else if (empty($arr)) {
                //get all Accounts
                $output = array('errorCode' => ErrorCode::successRead);
                $dataArr = array();
                foreach ($accountMO as $key => $account) {
                    $digest = hash('md5', ($account->accName) . ($account->password));
                    $item = array(
                        'accName' => $account -> accName,
                        'dataHash' => $digest
                    );
                    $dataArr[] = $item;
                }
                $output['results'] = $dataArr;
            }
        }
        return $output;
    }

    public function actionHandler($arr){
        $acc = Account::init($arr);
        if ($acc -> accName == NULL) {
            $outcome = ErrorCode::failCreate;
        } else if ($arr[':dataHash']) {
            $outcome = $this -> integrityValidator($arr[':accName'], $arr[':dataHash']);
            if ($outcome != ErrorCode::successRead){
                return array($outcome);
            }
            $outcome = ErrorCode::failUpdate;
        } else {
            return array(ErrorCode::failDelete);
        }
        $acc -> accName = $arr[':accName'];
        $acc -> password = $arr[':password'];
        if ($acc -> save() !=0) {
            $outcome -= 1;
        }
        return array($outcome);
    }

    public function relatedEntityHandler($arr) {
        $outcome = ErrorCode::failUpdate;
        $acc = Account::init($arr);
        if(!empty($acc)) {
            $acc -> empStatus = $arr[':empStatus'];
            $output = $acc -> saveEmployeeEntity();
            if ($output ==1) {
                $outcome = ErrorCode::successUpdate;
            }
        }
        return array($outcome);
    }

    public function deleteHandler($arr) {
        //dashboard
        $accName = $arr[':accName'];
        $dataHash = $arr['dataHash'];

        $valid = $this->integrityValidator($accName, $dataHash);
        if ($valid != ErrorCode::successRead) {
            return array($valid);
        }

        $deletion = Account::delete(array(':accName' => $accName));
        if ($deletion == 0) {
            return array(ErrorCode::failDelete);
        }

        return array(ErrorCode::successDelete);
    }

    private function integrityValidator($accName, $dataHash) {
        //account existence check
        $accountcheck = $this->requestHandler(array(':accName' => $accName));
        if ($accountcheck['errorCode'] != ErrorCode::successRead) {
            return ErrorCode::integFailCreate;
        }

        if ($accountcheck['results']['dataHash'] != $dataHash){
            return ErrorCode::integFailCreate;
        }

        return ErrorCode::successRead;
    }

    public function authenticateValidator($authUsername, $authPass) {
        $password = $this->requestHandler(array(':accName' => $authUsername, ':password' => $authPass));

        if ($password == $authPass) {
            return true;
        } else {
            return false;
        }
    }

    public function managerValidator($authUsername) {
        $status = $this->requestHandler(array(':accName' => $authUsername, ':empStatus' => 'Manager'));
        if ($status == 'Manager') {
            return true;
        } else {
            return false;
        }
    }

}

?>
